#include "LocalString.h"
#include "CCFileUtils.h"

void LocalString::parse( const char* filePath )
{
	const char* path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(filePath);

	xmlDoc = xmlReadFile(path, "utf-8", XML_PARSER_EOF);
	if(NULL == xmlDoc)
		CCLOG("LocalString xml parse failed!");

	rootNode = xmlDocGetRootElement(xmlDoc);
	if(NULL == xmlDoc)
		CCLOG("LocalString xml root null!");

	childNode = rootNode->children;
}

const char* LocalString::getLocalizationString( const char* id )
{
	xmlNodePtr curNode = childNode;
	while(NULL != curNode)
	{
		xmlChar* ids = xmlGetProp(curNode,(const xmlChar*)"id");
		if(xmlStrEqual((xmlChar*)id, ids))
		{
			xmlFree(ids);
			return attriToChar(curNode, "value");
		}
		xmlFree(ids);
		curNode = curNode->next;
	}
	return NULL;
}

LocalString::LocalString()
{
	parse("config/LocalString.xml");
}

LocalString::~LocalString()
{
	xmlFreeNode(childNode);
	xmlFreeDoc(xmlDoc);
}
